Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
simple-node-logger
Advanced tools
A node console and file logger suitable for small, medium and large production projects.
__ _ _ __ _ __
/ _(_)_ __ ___ _ __ | | ___ /\ \ \___ __| | ___ / / ___ __ _ __ _ ___ _ __
\ \| | '_ ` _ \| '_ \| |/ _ \ / \/ / _ \ / _` |/ _ \ / / / _ \ / _` |/ _` |/ _ \ '__|
_\ \ | | | | | | |_) | | __/ / /\ / (_) | (_| | __/ / /__| (_) | (_| | (_| | __/ |
\__/_|_| |_| |_| .__/|_|\___| \_\ \/ \___/ \__,_|\___| \____/\___/ \__, |\__, |\___|_|
|_| |___/ |___/
A simple multi-level logger for console, file, and rolling file appenders. Features include:
npm install simple-node-logger --save
// create a stdout console logger
const log = require('simple-node-logger').createSimpleLogger();
or
// create a stdout and file logger
const log = require('simple-node-logger').createSimpleLogger('project.log');
or
// create a custom timestamp format for log statements
const SimpleNodeLogger = require('simple-node-logger'),
opts = {
logFilePath:'mylogfile.log',
timestampFormat:'YYYY-MM-DD HH:mm:ss.SSS'
},
log = SimpleNodeLogger.createSimpleLogger( opts );
or
// create a file only file logger
const log = require('simple-node-logger').createSimpleFileLogger('project.log');
or
// create a rolling file logger based on date/time that fires process events
const opts = {
errorEventName:'error',
logDirectory:'/mylogfiles', // NOTE: folder must exist and be writable...
fileNamePattern:'roll-<DATE>.log',
dateFormat:'YYYY.MM.DD'
};
const log = require('simple-node-logger').createRollingFileLogger( opts );
or
// create a log manager
const manager = require('simple-node-logger').createLogManager();
manager.createConsoleAppender();
const log = manager.createLogger('MyClass');
// create other logs and appenders...
The first use simply logs to the console. The second logs to the console and to the project.log file. The third create a console logger with a custom timestamp format. The fourth logs to the file only. The fifth creates a rolling file log system in the target log folder. The fifth creates a log manager to enable you to add various appenders with multiple levels and create logs for each module or class.
See the examples folder for in depth samples...
The log levels include the standard set: trace, debug, info, warn, error and fatal. The default level is info. The log level can be set at run-time by doing this:
log.setLevel('warn');
This sets the log level to warn and suppresses debug and info messages.
The default format is HH:mm:ss.SSS LEVEL message. For example, the log message:
log.info('subscription to ', channel, ' accepted at ', new Date().toJSON());
Yields:
14:14:21.363 INFO subscription to /devchannel accepted at 2014-04-10T14:20:52.938Z
If you create a logger with a category name, all log statements will include this category. Typically a category is a class or module name. If you create a logger with the category name 'MyCategory', the log statement would format like this:
14:14:21.363 INFO MyCategory subscription to /devchannel accepted at 2014-04-10T14:20:52.938Z
You can create a single logger / log manager and add multiple appenders with different log levels. For example, you can add a console appender that has a log level of warn and a file appender to debug.
See examples/category-logger.js for an example.
Writes to the console. This is the simplest appender typically used for command line applications or for development.
Writes to the specified file. This appender is typically used for services that periodically start and stop or that have a limited number of log statements. An example would be to log just error & fatal messages separate from other logs.
The rolling file appender offers a full production logger where files roll based on date and time. The minimum roll time is a single hour. A typical application would be a production environment where log files are rolled throughout the day then archived to a separate location.
The rolling file appender requires a valid date format and file name pattern. The filename must contain the key word that will be replaced with the formatted date. The configuration must also include a target log directory where the files will be written.
mylog-<DATE>.log
ApplicationName.log.<DATE>
<DATE>.log
<DATE>
Date formats must map to acceptable file names so have more restrictions than typical dates. If you use delimiters, you are restricted to a dash or dot delimiter to separate year, month, day and hour. Valid examples include:
MMDD // simple month day that rolls at midnight (no delimiters)
YYYY.MM.DD-HH // year month day and hour that can roll up to once per hour
YYYY-MM-DD.a // year month day and am/pm that rolls twice per day
YYYY-MMM-DD // year month day where month is the short name (Mar, Apr, etc)
The default format YYYY.MM.DD is used if the format is not supplied.
Create a javascript configuration that implements 'readConfig' to return configuration details.
The examples folder includes a handful of simple to not so simple cases for console, file, multi-appender, category, etc.
Adding a new appender is as easy as implementing write( logEntry ). The easiest way to implement is by extending the base class AbstractAppender. You may also easily override the formatting, order, etc by overriding or providing your own abstract or concrete appender.
For example, you can extend the AbstractAppender to create a JSON appender by doing this:
const AbstractAppender = require('simple-node-logger').AbstractAppender;
const JSONAppender = function() {
'use strict';
var appender = this;
var opts = {
typeName:'JSONAppender'
};
AbstractAppender.extend( this, opts );
// format and write all entry/statements
this.write = function(entry) {
var fields = appender.formatEntry( entry );
process.stdout.write( JSON.stringify( entry ) + '\n' );
};
};
The appenders have formatting messages that can be overridden at the abstract or concrete level. The format methods include:
It's easy to extend any one of the log methods at the instance level. Here is an example of overriding the error log to send a socket message:
const log = new require('simple-node-logger').createSimpleLogger();
const socket = openWebSocket();
// override the standard error method to send a socket message
log.error = function() {
var args = Array.prototype.slice.call( arguments ),
entry = log.log('error', args);
// now do something special with the log entry...
process.nextTick(function() {
socket.send( JSON.stringify( entry ));
});
};
All unit tests are written in mocha/chai/should and can be run from the command line by doing this:
make test
There is also a file watcher that can be invoked with this:
make watch
Mocks used for testing include MockLogger and MockAppender. Typically you would use MockLogger for unit tests like this:
const MockLogger = require('simple-node-logger').mocks.MockLogger;
const log = MockLogger.createLogger('MyCategory');
log.info('this is a log statement');
log.getLogEntries().length.should.equal( 1 );
MockLogger extends Logger and uses MockAppender to capture log entries.
Apache 2.0
Copyright © 2014-2019, rain city software | Version 18.12.24
FAQs
A node console and file logger suitable for small, medium and large production projects.
The npm package simple-node-logger receives a total of 27,073 weekly downloads. As such, simple-node-logger popularity was classified as popular.
We found that simple-node-logger demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.